import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParserHandler extends DefaultHandler {
	private HashMap<String, String> columns = new HashMap<String, String>(); // columnName=value
	private List<HashMap<String, String>> tb = new ArrayList<HashMap<String, String>>();
	
	private String currentColumn; 
	private boolean isBody = false;
	private boolean isRow = false;
	Connection conn = null;
	private String tmpValue;

	public HashMap<String, String> getColumns() {
		return columns;
	}

	public void setColumns(HashMap<String, String> columns) {
		this.columns = columns;
	}


	public void setTb(List<HashMap<String, String>> tb) {
		this.tb = tb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
	 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equalsIgnoreCase("BODY")) {
			System.out.println("[[...Body parsing start...]]");
			isBody = true;
		} else if (isBody && qName.equalsIgnoreCase("row")) {
			isRow = true;
		} else {
			currentColumn = qName;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	public void characters(char ch[], int start, int length)
			throws SAXException {
		tmpValue = new String(ch, start, length);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("BODY")) {
			System.out.println("[[...Body parsing end...]]");

			isBody = false;
		} else if (isBody && qName.equalsIgnoreCase("row")) {
			isRow = false;
			tb.add(columns);
			columns = new HashMap<String, String>();
			System.out.println("--------------- row add!! -------------");
		} else {
			columns.put(currentColumn, tmpValue);
			System.out.println("--------- col  " + currentColumn.trim());
			System.out.println("--------- val  " + tmpValue.trim());
			// init
			tmpValue = "";
		}
	}


	public List<HashMap<String, String>> getTb() {
		return tb;
	}
}